<template>
<div>
	<content_top title="固定资产" isAccounts="1"></content_top>
	
	<div class="g_box">
		<div class="g_head">
			<h3>筛选查询</h3>
			<div class="g_box_right">
				<ButtonGroup class="g_act_box">
					<Button @click="query">查询结果</Button>
				</ButtonGroup>
				<div class="g_other">
					<a @click="clear_data">清除筛选</a>
				</div>
			</div>
		</div>
		<div class="g_box_con" style="padding:20px;">
			<Form  :label-width="110" style="width: 100%" inline>
				<FormItem label="公司主体：">
					<company_list v-model="filter.company" field="sx_name" style="width:150px;"></company_list>
				</FormItem>
				<FormItem label="承担部门：">
					<bumen_filter v-model="filter.bumen" field="name" style="width:150px;"></bumen_filter>
				</FormItem>
				<FormItem label="状态：">
					<Select v-model="filter.status" style="width:180px" placeholder="请选择">
						<Option value="" >全部</Option>
						<Option :value="0" >在用</Option>
						<Option :value="1" >报废</Option>
						<Option :value="2" >折旧完成</Option>
					</Select>
				</FormItem>
				<!-- <FormItem label="时间：">
					<DatePicker type="month" v-model="filter.date_month" placeholder="选择月份" style="width: 150px" @on-change="month_change"></DatePicker>
				</FormItem> -->
				<FormItem label="币种：">
					<currency_filter v-model="filter.bz" field="name" style="width:150px;"></currency_filter>
				</FormItem>
				<FormItem label="关键词搜索：">
					<Input v-model="filter.key_val" style="width:150px" placeholder="请输入关键词" ></Input>
				</FormItem>
			</Form>
		</div>
	</div> 
	
		<ButtonGroup class="btns_group">
			<!-- <Button :class="{active_btn:filter.subject=='傢俬設備'}" @click="set_type('傢俬設備')">固定资产</Button>
			<Button :class="{active_btn:filter.subject=='汽車'}" @click="set_type('汽車')">汽车</Button>
			<Button :class="{active_btn:filter.subject=='裝修'}" @click="set_type('裝修')">长期摊销</Button> -->

			<Button :class="{active_btn:filter.subject=='6043傢俬設備'}" @click="set_type('6043傢俬設備')">固定资产</Button>
			<Button :class="{active_btn:filter.subject=='6042汽車'}" @click="set_type('6042汽車')">汽车</Button>
			<Button :class="{active_btn:filter.subject=='6041裝修'}" @click="set_type('6041裝修')">长期摊销</Button>

		</ButtonGroup>
	<div class="g_box" style="margin-top: 0;">
		
		<div class="g_head">
			<h3>数据列表</h3>
			<div class="g_box_right">
				<ButtonGroup class="g_act_box">
					<Button  @click="export_data">导出表格</Button>
					<Button @click="import_entry">引入资产</Button>
				</ButtonGroup>
			</div>
		</div>
		<div class="g_box_con">
			<Table ref="table" :columns="Columns" :data="list" :border="true"></Table>
			<div style="font-size: 14px;overflow: hidden;padding: 10px;">
				<div style="float:left;">
					<Select v-model="batch_act" style="width:100px;;">
						<Option value="" >请选择</Option>
						<Option value="baofei">报废</Option>
						<Option value="zaiyong">在用</Option>
						<Option value="zhejiu_ok">折旧完成</Option>
						<Option value="delete" >删除</Option>
					</Select>
					<Button @click="batch_action">确定</Button>
				</div>
				<div style="float:right;">
					<Page :total="count" show-elevator show-total @on-change="goto_page" />
				</div>
			</div>
		</div>
	</div>
	
	<!-- 引入资产 -->
	<yinru_box v-model="yinru.show" :cinfo="filter" @success="success_change" subject="引入资产"></yinru_box>
	
	<!-- 编辑 -->
	<info_box v-model="einfo.show"  :cinfo="einfo.info" @success="success_change"></info_box>

</div>
</template>

<script>
import content_top from '@/themes/components/content_top';
import info_box from './info';
import yinru_box from '../yinru';
import company_list from '@/themes/components/company_list';
import bumen_filter from '@/themes/components/bumen_filter';
import currency_filter from '@/themes/components/currency_filter';
export default {
    name: 'fixed_assets_list',
    data () {
		let self = this;
        return {
			yinru:{
				show:false,
				subject:''
			},
			einfo:{
				show:false,
				info:{}
			},
            Columns: [
				{
					type: 'selection',
					width: 60,
					align: 'center'
				},
				{
					title: '固定资产编号',
					key: 'assets_no',
					align: 'center',
                    width: 80,
                },
				{
                    title: '公司主体',
					key: 'company',
					align: 'center',
					width: 100,
                },
				
				{
                    title: '部门',
					key: 'bumen',
					align: 'center',
					width: 80,
                },
				{
                    title: '地区',
					key: 'region',
					align: 'center',
					width: 80,
					
                },
				{
                    title: '使用人',
					key: 'user',
					align: 'center',
					width: 80,
                },
				{
                    title: '固定资产名称',
					key: 'name',
					align: 'center',
					width: 80,
                },
				{
                    title: '单位',
					key: 'num',
					align: 'center',
					width: 80,
                },
				{
                    title: '取得时间',
					key: 'start_date',
					align: 'center',
					width: 100,
                },
				{
                    title: '截止时间',
					key: 'end_date',
					align: 'center',
					width: 100,
                },
				{
                    title: '使用年限',
					key: 'useyear',
					align: 'center',
					width: 100,
					render: (h, params) => {
					
						var txt = '';
						if(params.row.useyear>0){
							txt += params.row.useyear+'年';
						}
						txt += params.row.usemonth+'个月';
						
						return h('div',txt);
					}
                },
				{
                    title: '固定资产原值',
					key: 'money',
					align: 'center',
					width: 80,
                },
                {
                    title: '币种',
					key: 'bz',
					align: 'center',
					width: 80,
                },
				{
                    title: '应提折旧',
					key: 'depreciation',
					align: 'center',
					width: 100,
                },
				{
                    title: '累计折旧期末余额',
					key: 'balance',
					align: 'center',
					width: 100,
                },
				{
                    title: '使用状态',
					key: 'status',
					align: 'center',
					width: 100,
					render: (h, params) => {
						var arr = ['在用','报废','折旧完成'];
						
						return h('div',arr[params.row.status]);
					}
                },
				{
					title:'操作',
					key: 'action',
					render: (h, params) => {
						return h('div', [
								h('Select', {
										props:{
											value: params.row.type
										},
										style:{ 
											width: '80px',
											marginRight: '5px'
											},
										on: {
											'on-change':(val) => {
												
												self.set_status(params.row.id,val);
											}
										},
									},
									[
										h('Option',{props: {value:0}},'在用'),
										h('Option',{props: {value:1}},'报废'),
										h('Option',{props: {value: 2}},'折旧完成'),
									]),
							h('Button', {
								props: {
									type: 'info',
									size: 'small',
								},
								style: {
									marginRight: '5px'
								},
								on: {
									click: () => {
										this.dbedit(params.row,params.row.id)
									}
								}
							}, '编辑'),
							h('Button', {
								props: {
									type: 'info',
									size: 'small',
								},
								style: {
									marginRight: '5px'
								},
								on: {
									click: () => {
										this.remove(params.row.id)
									}
								}
							}, '删除')
						]);
					}
				}
            ],
			filter:{
				company:'',
				bumen:'',
				status:0,
				useless_date:localStorage.account_balance?localStorage.account_balance:'',
				bz:'all',
				keyword:'',
				subject:'6043傢俬設備',
			},
			default_filter:{
				company:'',
				bumen:'',
				status:0,
				useless_date:'',
				bz:'all',
				keyword:'',
				subject:'6043傢俬設備',
			},
			list:[],
			page:1,
			count:0,
			batch_act:'',
        };
	},
	components:{
		content_top,
		info_box,
		yinru_box,
		company_list,
		bumen_filter,
		currency_filter
	},
	mounted () {
		
		let btns = document.querySelectorAll('.btns_group button');
		
		for(let i = 0;i < btns.length;i ++){
			btns[i].onclick = function(e){

				for (let j = 0; j < btns.length; j++) {
					btns[j].classList.remove('active_btn');
				}
				btns[i].classList.add('active_btn');
			};
		}
	},
	created(){
		this.set_type('6043傢俬設備');
		
	},
    computed: {
       
    },
    methods:{
		set_type(type){
			this.filter.subject = type;
			this.load_data();
		},
		load_data(){

			var self = this;
			var postdata = {
				f:this.filter,
				page:this.page,
				//current_month:localStorage.account_balance
			};
			
			Whttp.load('Accounting/Assets/get_list',postdata,function(result){//console.log(result);
				self.list = result.list;
				self.count = parseInt(result.count)
			})
		},

		goto_page(page){
			this.page = page;
			this.load_data();
		},
		query(){
			this.page = 1;
			this.load_data();
		},
		month_change(date){
			this.filter.date_month =  date;
		},
		// 引入资产
		import_entry(){
			this.yinru.show = true;
		},
		dbedit(row,index){
			this.einfo.info = this._getData(row);
			this.einfo.show = true;
		},
		remove(id){
			var self = this;
			if(!confirm('是否删除')){
				return false;
			}
			var postdata = {
				id:id
			};
			Whttp.load('Accounting/Assets/del',postdata,function(result){
				self.load_data();
				self.$Message.info('删除成功');
			});
		},
		batch_action(){
			var ids = [];
			if(this.batch_act=='delete'){
				if(!confirm('是否批量删除')){
					return false;
				}
			}
			
			var sels = this.$refs['table'].getSelection();
			
			for(var i=0;i<sels.length;i++){
				ids.push(sels[i].id);
			}
			
			var self = this;
			var postdata = {
				ids:ids,
				current_month:localStorage.account_balance,
				act:this.batch_act
			};
			Whttp.load('Accounting/Assets/batch_act',postdata,function(result){
				self.load_data();
				self.$Message.info('操作成功');
				self.batch_act = '';
			});
			
		},
		success_change(result){
			this.load_data();
		},
		set_status(id,status){
			var self = this;
			var postdata = {
				id:id,
				status:status,
				current_month:localStorage.account_balance
			};
			Whttp.load('Accounting/Assets/set_status',postdata,function(result){
				self.load_data();
				self.$Message.info('设置成功');
			});
		},
		export_data(){
			var self = this;
			
			var name= '';
			if(this.filter.subject=='傢俬設備'){
				name = '固定资产';
			}
			else if(this.filter.subject=='汽車'){
				name = '汽车';
			}
			else if(this.filter.subject=='裝修'){
				name = '长期摊销';
			}
			
			var postdata = {
				f:this.filter,
				name:name
			};
			
			Whttp.load('Accounting/Assets/export_data',postdata,function(result){
				window.open(result);
				self.$Message.info('导出成功');
			})
		},
		//清除筛选
		clear_data(){
			this.filter = this._getData(this.default_filter);
			this.page = 1;
			this.load_data();
		}
	}
};
</script>
<style scoped>
.btns_group{
	margin: 0 15px;
}
.btns_group button{
	width: 100px;
	border-radius: 0;
}
.btns_group button:hover{
	border: 1px solid #1abc9c;
}
.btns_group .active_btn{
	color: #fff;
	border: 1px solid #1abc9c;
    box-shadow: none;
	background-color: #1abc9c;
}
</style>